package ch.fhnw.qualiscan.application.elements;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.modals.AdminDeleteQuestion;
import ch.fhnw.qualiscan.application.modals.AdminEditQuestion;
import ch.fhnw.qualiscan.application.modals.AdminNewQuestion;
import ch.fhnw.qualiscan.application.models.AdminModel;
import ch.fhnw.qualiscan.application.views.AdminQuestionnaireView;
import ch.fhnw.qualiscan.core.db.pojos.Language;
import ch.fhnw.qualiscan.core.db.pojos.Question;
import ch.fhnw.qualiscan.core.libraries.ModalLibrary;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class AdminQuestionForm extends HorizontalLayout {
	
	public Question question;
	public int numbering;
	public int answer = 0;
	public boolean isSubquestion;
	public AdminQuestionnaireView caller;
	private AdminModel adminModel;
	private Language language;
	private ModalLibrary modalLib;
	
	
	/**
	 * This function displays a question as a standalone question.
	 * @param question the question
	 * @param numbering number of the question
	 */
	public AdminQuestionForm(final Question question, int numbering, Language language, boolean isSubquestion, AdminQuestionnaireView caller) {
		
		this.numbering = numbering;
		this.question = question;
		this.isSubquestion = isSubquestion;
		this.caller = caller;
		this.language = language;
		adminModel = new AdminModel();
		modalLib = new ModalLibrary();
		
		buildLayout();
		
	}
	
	public void buildLayout() {
		
		removeAllComponents();
		
		setWidth("100%");
		setMargin(true);
		
		Label questionLabel = new Label(adminModel.getQuestionText(question, language).getText());
		Label numb = new Label("" + numbering);
		numb.setWidth("50px");
		
		if(isSubquestion == true) {
			setStyleName("subquestion");
			numb.setStyleName("normalNumber");
		} else {
			setStyleName("noSubquestion");
			numb.setStyleName("bigNumber");
		}
		
		addComponent(numb);
		addComponent(questionLabel);
		
		setExpandRatio(questionLabel, 1.0f);
		
		HorizontalLayout formWrap = new HorizontalLayout();
		formWrap.setSpacing(true);
		
		Button editQuestion = new Button(TM.get("action.edit"));
		editQuestion.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				modalLib.openModal(new AdminEditQuestion(question, (AdminQuestionForm)event.getButton().getParent().getParent()));
			}
		});
		editQuestion.setIcon(Config.ICON_EDIT);
		
		Button deleteQuestion = new Button(TM.get("action.delete"));
		deleteQuestion.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				if(isSubquestion == false) {
					modalLib.openModal(new AdminDeleteQuestion(question, caller, false));
				}
				else {
					modalLib.openModal(new AdminDeleteQuestion(question, caller, true));
				}
			}
		});
		deleteQuestion.setIcon(Config.ICON_CANCEL);
		
		if(isSubquestion == false) {
			Button addSubQuestion = new Button(TM.get("admin.questionform.subquestion"));
			addSubQuestion.addListener(new Button.ClickListener() {
				public void buttonClick(ClickEvent event) {
					modalLib.openModal(new AdminNewQuestion(question, caller));
				}
			});
			addSubQuestion.setIcon(Config.ICON_ADD);
			formWrap.addComponent(addSubQuestion);
		}
		formWrap.addComponent(editQuestion);
		formWrap.addComponent(deleteQuestion);
		
		addComponent(formWrap);
	}
	
}
